package com.auxgroup.smarthome.inner.entity;

import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.entity.AbstractGenericEntity;
import com.auxgroup.smarthome.inner.dto.AppIndexPageDto;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by niuGuangzhe on 2017/8/2.
 */
@Entity
@Table(name = "app_index_page")
public class AppIndexPageEntity extends AbstractGenericEntity {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(length = 36)
    private String indexPageId;

    @Column(nullable = false)
    private String zipUri;

    @Column(nullable = false)
    private String htmlUri;

    @Column(length = 2, nullable = false)
    //1:保存 2:发布 3:历史
    private Integer status;

    @Column(length = 20, nullable = false)
    //版本号
    private String version;

    private Long releaseTime;

    @Column
    private String releaseUser; //发布人


    public String getIndexPageId() {
        return indexPageId;
    }

    public void setIndexPageId(String indexPageId) {
        this.indexPageId = indexPageId;
    }

    public String getHtmlUri() {
        return htmlUri;
    }

    public void setHtmlUri(String htmlUri) {
        this.htmlUri = htmlUri;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Long getReleaseTime() {
        return releaseTime;
    }

    public void setReleaseTime(Long releaseTime) {
        this.releaseTime = releaseTime;
    }

    public String getReleaseUser() {
        return releaseUser;
    }

    public void setReleaseUser(String releaseUser) {
        this.releaseUser = releaseUser;
    }

    public String getZipUri() {
        return zipUri;
    }

    public void setZipUri(String zipUri) {
        this.zipUri = zipUri;
    }

    /**
     * 保存app首页管理
     * @param zipUri
     * @param htmlUri
     * @return
     */
    public AppIndexPageEntity createIndexVersion(String zipUri, String htmlUri) {
        this.zipUri = zipUri;
        this.htmlUri = htmlUri;
        this.status = 1;
        this.releaseTime = 0L;
        this.version = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))+ ThreadLocalRandom.current().nextInt(1000);
        return this;
    }

    /**
     * 发布app首页管理
     * @param releaseUser
     * @return
     */
    public AppIndexPageEntity releaseIndexVersion(String releaseUser) {
        this.releaseUser = releaseUser;
        this.status = 2;
        this.releaseTime = System.currentTimeMillis()/1000;
        return this;
    }

    /**
     * 发布新app首页管理, 原有版本发布变为历史版本
     * @return
     */
    public AppIndexPageEntity recordIndexVersion() {
        this.status = 3;
        return this;
    }


    public static AppIndexPageEntity translateToEntity(AppIndexPageDto dto, Integer status) {
        AppIndexPageEntity entity = BeanUtils.copyAttrs(new AppIndexPageEntity(), dto);
        entity.status = status;
        return entity;
    }

}
